/**
 * Created by shaoting on 16/10/17.
 */
import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    ListView,
    Image,
    TouchableOpacity,
    Platform,
    Dimensions,
    WebView,
    AsyncStorage
} from 'react-native';

var {width,height} = Dimensions.get('window').height; //屏宽
var NavigatorBar = require('../Main/NXNavigationBar') //NavigationBar
var DetailsWebView = React.createClass({
    componentDidMount(){
        var token = '';
        try {
            AsyncStorage.getItem(
                'token',
                (error,result)=>{
                    if (error){
                        alert('取值失败:'+error);
                    }else{
                        // alert('取值成功:'+result);
                        this.token = result
                    }
                }
            )
        }catch(error){
            alert('失败'+error);
        }
        let formData = new FormData();
        formData.append("articleId",'PA-5ca419d7-d105-41ef-bae9-2588a93e16c5');
        var fetchOptions = {
            method: 'POST',
            headers: {
                'token': this.token
            },
            body:formData,
        };

        let url = 'http://www.pintasty.cn/productdetail/findproductarticlebyid';
        fetch(url, fetchOptions)
            .then((response) => response.json())
            .then((json) => {
                console.log(json)
                if(json.status == 200){
                    //修改html
                    var html = json.data.content
                    html = html.replace(/&#34;/g,'"')
                    html = html.replace(/\/photo/g,'http://www.pintasty.cn/photo')
                    // alert(html)
                    this.setState({
                       html:html
                    })
                }else  if (json.status == 301){
                    alert('没有token')
                }else {
                    alert('访问服务器失败')
                }
            }) .done();
    },
    getInitialState(){
        return{
            html:''
        }
    },
    render() {
        return (
            <View style={styles.container}>
                <NavigatorBar title='webView'  widthWH={Platform.OS == 'ios' ? 15:15} leftBtnName = 'return_icon' rightBtnName = 'X123' leftpress={()=>this.leftAction()} rightPress={()=>this.rightAction()}  />
                <WebView
                    html={this.state.html}
                    javaScriptEnabled = {true}
                />
            </View>
        );
    },

    leftAction(){
        this.props.navigator.pop({

            }
        );
    },
    rightAction(){

    }
});


var styles = StyleSheet.create({
    container:{
        flex:1
    }
});

module.exports = DetailsWebView;